package com.example.eazytransfer.ui;

import com.example.eazytransfer.model.TCPUtil;
import com.example.eazytransfer.model.ViewModel;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;

import java.net.URL;
import java.util.ResourceBundle;

public class CreateConnectController implements Initializable {
    @FXML
    private RadioButton rbService;
    @FXML
    private RadioButton rbClient;
    @FXML
    private TextField tfServicePort;
    @FXML
    private Button btnStartService;
    @FXML
    private Label labelConnectStatus1;
    @FXML
    private TextField tfClientPort;
    @FXML
    private TextField tfServiceIP;
    @FXML
    private Button btnConnectService;
    @FXML
    private Label labelConnectStatus2;

    @FXML
    private Label labelServiceIP;
    @FXML
    private Button btnDisconnectService;
    @FXML
    private Button btnCloseService;

    private ToggleGroup toggleGroup;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //单选组
        toggleGroup = new ToggleGroup();
        rbService.setToggleGroup(toggleGroup);
        rbClient.setToggleGroup(toggleGroup);
        toggleGroup.selectedToggleProperty().addListener((observableValue, toggle, t1) -> {
            RadioButton r = (RadioButton) t1;
            if (r.getText().equals("服务端")) {
                ViewModel.getInstance().isServerModel.set(true);
            } else {
                ViewModel.getInstance().isServerModel.set(false);
            }
        });
        rbService.selectedProperty().bindBidirectional(ViewModel.getInstance().isServerModel);
        rbService.selectedProperty().addListener((observableValue, aBoolean, t1) -> {
            if (t1) {
                btnConnectService.setDisable(true);
                btnDisconnectService.setDisable(true);
                btnStartService.setDisable(false);
                btnCloseService.setDisable(true);
                tfServiceIP.setDisable(true);
                tfClientPort.setDisable(true);
                tfServicePort.setDisable(false);
            } else {
                btnConnectService.setDisable(false);
                btnDisconnectService.setDisable(true);
                btnStartService.setDisable(true);
                btnCloseService.setDisable(true);
                tfServiceIP.setDisable(false);
                tfClientPort.setDisable(false);
                tfServicePort.setDisable(true);
            }
        });
        ViewModel.getInstance().isServerStart.addListener((observableValue, aBoolean, t1) -> {
            if (t1) {
                btnStartService.setDisable(true);
                btnCloseService.setDisable(false);
                rbClient.setDisable(true);
            } else {
                btnStartService.setDisable(false);
                btnCloseService.setDisable(true);
                rbClient.setDisable(false);
            }
        });
        ViewModel.getInstance().isConnected.addListener((observableValue, aBoolean, t1) -> {
            if (ViewModel.getInstance().isServerModel.get()) {
                if (t1) {
                    labelConnectStatus1.setText("已连接");
                } else {
                    labelConnectStatus1.setText("未连接");
                }
            }else {
                if (t1) {
                    labelConnectStatus2.setText("已连接");
                    rbService.setDisable(true);
                    btnConnectService.setDisable(true);
                    btnDisconnectService.setDisable(false);
                } else {
                    labelConnectStatus2.setText("未连接");
                    rbService.setDisable(false);
                    btnConnectService.setDisable(false);
                    btnDisconnectService.setDisable(true);
                }
            }
        });
        labelServiceIP.textProperty().bind(ViewModel.getInstance().serviceIP);
        ViewModel.getInstance().serviceIP.set("0.0.0.0");
        tfServicePort.textProperty().bindBidirectional(ViewModel.getInstance().servicePort);
        ViewModel.getInstance().servicePort.set("5000");
        tfServiceIP.textProperty().bindBidirectional(ViewModel.getInstance().connectIP);
        tfClientPort.textProperty().bindBidirectional(ViewModel.getInstance().connectPort);
        ViewModel.getInstance().connectPort.set("5000");
        ViewModel.getInstance().isConnected.set(false);
        ViewModel.getInstance().isServerStart.set(false);
        ViewModel.getInstance().isServerModel.set(true);
    }

    @FXML
    protected void startService() {
        ViewModel.getInstance().isServerStart.set(true);
        ViewModel.getInstance().serviceIP.set(TCPUtil.getInstance().getHostAddress());
        TCPUtil.getInstance().startService();
    }

    @FXML
    protected void connectService() {
        TCPUtil.getInstance().connectService();
    }

    @FXML
    protected void closeService() {
//        TCPUtil.getInstance().sendMsg(Flags.disconnect);
        TCPUtil.getInstance().close();
        ViewModel.getInstance().isServerStart.set(false);
        ViewModel.getInstance().isConnected.set(false);
    }

    @FXML
    protected void disconnect() {
        TCPUtil.getInstance().close();
        ViewModel.getInstance().isConnected.set(false);
    }
}
